<?php

use App\Models\Business;
use App\Models\Role;
use Illuminate\Database\Seeder;

class RoleTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    //获取所有业务类型
    public function run()
    {
        //
        $allBusiness = Business::all();
        foreach ($allBusiness as $business){
            //判断角色是否存在
            $role = Role::where('name','=',$business->name.'责任人')->first();
            if ($role != null){
                continue;
            }
            echo $business->name.'\n';
            //根据项目生成角色
            $role = Role::create([
                'name' => $business->name.'责任人',
                'slug' => $business->name.'责任人',
                'type' => '业务类角色'
            ]);
            //更新business
            $business->role_id = $role->id;
            $business->save();
        }
    }
}
